package library;

import java.util.Set;
import java.util.TreeMap;

import library.item.LibraryItem;

public class BasicLibrary<T extends LibraryItem> implements Library<T> {

	public BasicLibrary() {
		library = new TreeMap<String, T>();
		counter = 0;
	}

	int counter;
	TreeMap<String, T> library;

	@Override
	public boolean addEntry(T e) {
		if (library.containsKey(e.getIdentifier()))
			return false;
		else {
			library.put(e.getIdentifier(), e);
			counter++;
			e.setInventoryNumber(counter);
			return true;
		}
	}

	public TreeMap<String, T> getLibrary() {
		return library;
	}

	@Override
	public boolean deleteEntry(String id) {
		if (library.containsKey(id)) {
			library.remove(id);
			return true;
		} else
			return false;
	}

	@Override
	public boolean emptyLibrary() {
		library.clear();
		counter = 0;
		return true;
	}

	@Override
	public T getEntry(String id) {
		if (library.containsKey(id)) {
			T e;
			e = library.get(id);
			return e;
		} else
			return null;
	}

	@Override
	public Set<String> getIdentifiers() {
		return library.keySet();
	}

	@Override
	public int getSize() {
		return library.size();
	}

	public String toString() {
		if (library.size() > 0) {
			StringBuilder string = new StringBuilder();
			for (T value : library.values()) {
				string.append(value.toString()).append("\n");
				string.trimToSize();
			}
			return string.toString();
		} else
			return "";
	}

}
